package com.nb.hdj.testCase.teamModule;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

/**
 * @Description: 团队管理 角色测试用例类
 * @Author: hedongjia
 * @Date: 2021/12/7 下午5:51
 * @Version: v1.0
 */
public class RoleTeamCase extends TeamCaseBase {
    //第一行部门员工姓名
    private String employeeName = "";
    //输入角色内容
    private String roleName="自动化测试角色";
    /**
     * 编辑员工角色
     */
    public boolean editEmployeeRole(WebDriver driver) {
        try {
            //点击高层领导
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("topLeadersAreaElement")));
            Thread.sleep(1500);
            //获取第一行部门员工姓名
            employeeName = seleniumUtil.getElementText(driver, By.xpath(teamMap.get("bJFitstEmployees")));
            //点击高层领导部门 第一行员工
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("bJFitstEmployees")));
            //点击编辑
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("editorButtonElement")));
            //员工角色下拉框
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("roleDownElement")));
            //选择角色为--测试角色
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("selectRoleElement")));
//            //点击确定
//            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("yesButtonElement")));
//            //点击返回
//            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("backButtonElement")));
            datemineAndReturn(driver,teamMap.get("yesButtonElement"));
            String roleName = "测试角色";
            if (seleniumUtil.elementTextIsContains(driver, By.xpath(teamMap.get("firstRoleElement")), roleName)) {
                return true;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除员工角色
     */
    public boolean deleteEmployeeRole(WebDriver driver) {
        try {
            //循环获取到employeeName与第一次编辑角色的名字所对应,因为每次编辑后，员工列表排序不一致
            for (int i = 1; i < 13; i++) {
                //每次循环得到的名字
                String cycleName = seleniumUtil.getElementText(driver, By.xpath("//*[@id=\"pane-2\"]/section/section[1]/div[2]/div[5]/div[2]/table/tbody/tr[" + i + "]/td[1]/div"));
                //String cycleName = seleniumUtil.getElementText(driver, By.xpath(("//*[@id=\"pane-2\"]/section/section[1]/div[2]/div[5]/div[2]/table/tbody/tr[{}]/td[1]/div",i)));
                if (employeeName.equals(cycleName)) {
                    //点击高层领导部门 所对应员工姓名
                    seleniumUtil.elementClick(driver, By.xpath("//*[@id=\"pane-2\"]/section/section[1]/div[2]/div[5]/div[2]/table/tbody/tr[" + i + "]/td[1]/div/a"));
                    //点击编辑
                    seleniumUtil.elementClick(driver, By.xpath(teamMap.get("editorButtonElement")));
                    Thread.sleep(500);
                    //删除员工角色
                    seleniumUtil.elementClick(driver, By.xpath(teamMap.get("deleteRoleElement")));
                    Thread.sleep(1000);
                    datemineAndReturn(driver,teamMap.get("yesButtonElement"));
                    String deleteRoleName = "-";
                    if (seleniumUtil.elementTextIsContains(driver, By.xpath("//*[@id=\"pane-2\"]/section/section[1]/div[2]/div[3]/table/tbody/tr["+i+"]/td[7]/div"), deleteRoleName)) {
                        return true;
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 添加角色
     * @param driver
     * @return
     */
    public boolean insertRole(WebDriver driver){

        try {
            //点击高层领导部门 第一行员工
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("bJFitstEmployees")));
            //点击编辑
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("editorButtonElement")));
            //点击添加角色
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("insertRolButtonElement")));
            //输入角色名称
            seleniumUtil.elementClickAndClearAndInput(driver,By.xpath(teamMap.get("inputRoleNameElement")),roleName);
            //选择权限 点击咨询
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("consultPermission")));
            //点击精品图库
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("photoPermission")));
            datemineAndReturn(driver,teamMap.get("determineButtonElement"));
            Thread.sleep(1000);
            //点击权限管理菜单
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("permissionMenuElement")));
            //点击角色管理
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("roleMenuElement")));
            //判断第一行角色名称是否为刚刚新增的角色名称
            if(seleniumUtil.elementTextIsContains(driver,By.xpath(teamMap.get("firstRoleName")),roleName)){
                return true;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }


    public boolean deleteRole(WebDriver driver){
        try {
            //点击删除按钮
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("roleDeleteElement")));
            //点击确定
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("roleDeleteYesElement")));
            //判断是否删除  刚新增的角色
            if(!seleniumUtil.elementTextIsContains(driver,By.xpath(teamMap.get("firstRoleName")),roleName)){
                return true;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 确定和返回操作封装
     * @param driver
     * @param determineButtonElement 确定按钮元素
     */
    public void datemineAndReturn(WebDriver driver,String determineButtonElement){
        try {
            //点击确定
            seleniumUtil.elementClick(driver, By.xpath(determineButtonElement));
            Thread.sleep(1000);
            //点击返回
            seleniumUtil.elementClick(driver, By.xpath(teamMap.get("backButtonElement")));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
